package com.qys.livesMall.shop.event.handle.service;

import com.alibaba.fastjson2.JSONObject;
import com.qys.livesMall.common.constants.Constants;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.shop.dao.H2ShopBaseDao;
import com.qys.livesMall.shop.event.handle.BaseHandler;
import com.qys.livesMall.shop.event.shop.pojo.ShopRechargeWithdrawalPojo;
import com.qys.livesMall.shop.modal.entity.H2ShopBase;
import com.qys.livesMall.shop.modal.entity.H2ShopStatisticsInfo;
import com.qys.livesMall.shop.modal.entity.ShopEventInfo;
import com.qys.livesMall.shop.modal.entity.ShopStatisticsEveryDay;
import com.qys.livesMall.shop.service.H2ShopBaseService;
import com.qys.livesMall.shop.service.H2ShopRechargeWithdrawalService;
import com.qys.livesMall.shop.service.H2ShopStatisticsInfoService;
import com.qys.livesMall.shop.service.ShopStatisticsEverydayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;

import static com.qys.livesMall.finance.enums.AccountBusinessTypeEnum.SHOP_RECHARGE;
import static com.qys.livesMall.finance.enums.AccountBusinessTypeEnum.SHOP_WITHDRAWAL;

/**
 * @menu ShopReWlServer
 * @date 2023/11/03 15:23
 */
@Service
public class ShopReWlServer implements BaseHandler<ShopRechargeWithdrawalPojo> {

    @Autowired
    private H2ShopStatisticsInfoService shopStatisticsInfoService;

    @Autowired
    private H2ShopRechargeWithdrawalService rechargeWithdrawalService;

    @Autowired
    private ShopStatisticsEverydayService shopStatisticsEverydayService;

    @Autowired
    private H2ShopBaseDao shopBaseService;
    @Override
    public void correct(Long shopId) {
        H2ShopStatisticsInfo shopStatisticsInfo=shopStatisticsInfoService.getField(shopId,H2ShopStatisticsInfo::getId, H2ShopStatisticsInfo::getWithdrawal,H2ShopStatisticsInfo::getRecharge,H2ShopStatisticsInfo::getWithdrawalNumber,H2ShopStatisticsInfo::getRechargeNumber);
        //统计总充值额/提现
        Map re=rechargeWithdrawalService.getTotalRe(shopId);
        Map wl=rechargeWithdrawalService.getTotalWl(shopId);
        //统计
        Integer reNum=0;
        BigDecimal totalRe=BigDecimal.ZERO;
        if (re!=null){
            if (re.get("num")!=null){
                reNum=(Integer)re.get("num");
            }
            if (re.get("totalRe")!=null){
                totalRe=(BigDecimal) re.get("totalRe");
            }
        }
        Integer wlNum=0;
        BigDecimal totalWl=BigDecimal.ZERO;
        if (re!=null){
            if (re.get("num")!=null){
                wlNum=(Integer)re.get("num");
            }
            if (re.get("totalWl")!=null){
                totalWl=(BigDecimal) re.get("totalWl");
            }
        }
        shopStatisticsInfo.setRecharge(totalRe);
        shopStatisticsInfo.setRechargeNumber(reNum);
        shopStatisticsInfo.setWithdrawal(totalWl);
        shopStatisticsInfo.setWithdrawalNumber(wlNum);
        shopStatisticsInfo.setDifference(shopStatisticsInfo.getRecharge().subtract(shopStatisticsInfo.getWithdrawal()));
        shopStatisticsInfoService.updateById(shopStatisticsInfo);
    }

    @Override
    public void handle(ShopEventInfo<ShopRechargeWithdrawalPojo> info) {
        H2ShopStatisticsInfo shopStatisticsInfo=shopStatisticsInfoService.getField(info.getShopId(),H2ShopStatisticsInfo::getId, H2ShopStatisticsInfo::getWithdrawal,
                H2ShopStatisticsInfo::getRecharge,
                H2ShopStatisticsInfo::getWithdrawalNumber,
                H2ShopStatisticsInfo::getRechargeNumber,
                H2ShopStatisticsInfo::getFirstCharge);
        if (shopStatisticsInfo==null){
            throw new QysException("未发现店铺："+info.getShopId()+"的统计数据");
        }
        if (info.getParamObj().getAmount()==null){
            throw new QysException("统计店铺提现错误："+ JSONObject.toJSONString(info));
        }
        if (shopStatisticsInfo.getWithdrawal()==null){
            shopStatisticsInfo.setWithdrawal(BigDecimal.ZERO);
        }
        if (shopStatisticsInfo.getWithdrawalNumber()==null){
            shopStatisticsInfo.setWithdrawalNumber(0);
        }
        if (shopStatisticsInfo.getRechargeNumber()==null){
            shopStatisticsInfo.setRechargeNumber(0);
        }
        if (shopStatisticsInfo.getRecharge()==null){
            shopStatisticsInfo.setRecharge(BigDecimal.ZERO);
        }
        ShopStatisticsEveryDay shopStatisticsEveryday=shopStatisticsEverydayService.getField(info.getShopId(), LocalDate.now(),
                ShopStatisticsEveryDay::getId,
                ShopStatisticsEveryDay::getReAmount,
                ShopStatisticsEveryDay::getWlAmount,
                ShopStatisticsEveryDay::getReNum,
                ShopStatisticsEveryDay::getWlNum,
                ShopStatisticsEveryDay::getDifference);
        if (shopStatisticsEveryday==null){
            shopStatisticsEveryday=new ShopStatisticsEveryDay();
            shopStatisticsEveryday.setShopId(info.getShopId());
            shopStatisticsEveryday.setDataDate(LocalDate.now());
            shopStatisticsEveryday.setReAmount(BigDecimal.ZERO);
            shopStatisticsEveryday.setWlAmount(BigDecimal.ZERO);
            shopStatisticsEveryday.setReNum(0);
            shopStatisticsEveryday.setWlNum(0);
            shopStatisticsEveryday.setDifference(BigDecimal.ZERO);
        }
        if (Objects.equals(info.getParamObj().getType(), SHOP_WITHDRAWAL.getCode())){
            shopStatisticsEveryday.setWlAmount(shopStatisticsEveryday.getWlAmount().add(info.getParamObj().getAmount()));
            shopStatisticsEveryday.setWlNum(shopStatisticsEveryday.getWlNum()+1);
            shopStatisticsInfo.setWithdrawal(shopStatisticsInfo.getWithdrawal().add(info.getParamObj().getAmount()));
            shopStatisticsInfo.setWithdrawalNumber(shopStatisticsInfo.getWithdrawalNumber()+1);

        }
        if (Objects.equals(info.getParamObj().getType(), SHOP_RECHARGE.getCode())){
            shopStatisticsEveryday.setReAmount(shopStatisticsEveryday.getReAmount().add(info.getParamObj().getAmount()));
            shopStatisticsEveryday.setReNum(shopStatisticsEveryday.getReNum()+1);
            shopStatisticsInfo.setRecharge(shopStatisticsInfo.getRecharge().add(info.getParamObj().getAmount()));
            shopStatisticsInfo.setRechargeNumber(shopStatisticsInfo.getRechargeNumber()+1);
            if (shopStatisticsInfo.getRechargeNumber()==1&&shopStatisticsInfo.getDifference()==null){
                shopStatisticsInfo.setFirstCharge(LocalDateTime.now());
            }
            if (shopStatisticsInfo.getRechargeNumber()==2){
                shopStatisticsInfo.setRechargeMoney(info.getParamObj().getAmount());
            }
            H2ShopBase shop=shopBaseService.getById(info.getShopId());
            if (!Objects.equals(shop.getRecharged(), Constants.YesNoInt.YES)){
                shop.setRecharged(Constants.YesNoInt.YES);
                shopBaseService.updateById(shop);
            }
        }
        shopStatisticsEveryday.setDifference(shopStatisticsEveryday.getReAmount().subtract(shopStatisticsEveryday.getWlAmount()));
        shopStatisticsEverydayService.saveOrUpdate(shopStatisticsEveryday);
        shopStatisticsInfo.setDifference(shopStatisticsInfo.getRecharge().subtract(shopStatisticsInfo.getWithdrawal()));
        shopStatisticsInfoService.updateById(shopStatisticsInfo);
    }
}

